package com.itboye.sunsun.luntan;
import java.util.ArrayList;
import java.util.List;

import com.itboye.sunsunhome.R;
import com.itboye.sunsun.adapter.ChaKanGengDuoPingLunAdapter;
import com.itboye.sunsun.beans.AllCommontBean;
import com.itboye.sunsun.beans.AllCommontBean.CommontItemBean;
import com.itboye.sunsun.beans.LunTanCommontBean;
import com.itboye.sunsun.beans.LunTanCommontBean.CommontBean;
import com.itboye.sunsun.beans.LunTanCommontBean.GenTieItemBean;
import com.itboye.sunsun.constants.NetPublicConstant;
import com.itboye.sunsun.custome.XListView;
import com.itboye.sunsun.custome.XListView.IXListViewListener;
import com.itboye.sunsun.network.HttpRequest;
import com.itboye.sunsun.support.BaseActivity;
import com.itboye.sunsun.utils.DensityUtil;
import com.itboye.sunsun.utils.XImageLoader;
import com.itboye.sunsun.volley.MyJsonRequest;
import com.itboye.sunsun.volley.XErrorListener;
import com.itboye.sunsun.volley.XRequestListener;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.TextView;

public class ChaKanGengDuoPingKuanActivity extends BaseActivity {

	XListView xlistview;
	private int pageNum=1;
	BaseAdapter adapter;
	List<CommontItemBean> dataList=new ArrayList<>();
	 @Override
	protected void onCreate(Bundle arg0) {
		
		super.onCreate(arg0);
		setContentView(R.layout.activity_cahkan_gengduo_pinglun);
		setStatusBarColor(R.color.top_blue);
		initView();

	 
		pullDown();
		
	}
	private void initView() {
		
		xlistview.setPullRefreshEnable(true);
		xlistview.setPullLoadEnable(true);
		xlistview.setXListViewListener(new IXListViewListener() {
			
			@Override
			public void onRefresh() {
				
				pullDown();
			}
			
			@Override
			public void onLoadMore() {
				
				pullUp();
			}
		});
		
		View headerView=getLayoutInflater().inflate(R.layout.item_luntan_gentie	, xlistview,false);
		Intent intent=getIntent();
		XImageLoader.load(NetPublicConstant.IMAGE_URL+intent.getStringExtra("head"), (ImageView)headerView.findViewById(R.id.head));
		
		((TextView)headerView.findViewById(R.id.name)).setText(intent.getStringExtra("nickname"));
		((TextView)headerView.findViewById(R.id.time)).setText(intent.getStringExtra("datelineShow"));
		((TextView)headerView.findViewById(R.id.content)).setText(intent.getStringExtra("message"));
		headerView.findViewById(R.id.delete).setVisibility(View.GONE);
		headerView.findViewById(R.id.pinglun).setVisibility(View.INVISIBLE);
		
		List<String>imgs=intent.getStringArrayListExtra("imgs");
		if (imgs!=null) {
			ViewGroup imgsContainer=(ViewGroup) headerView.findViewById(R.id.imgsContainer);
			int width=DensityUtil.dip2px(80);
			int height=DensityUtil.dip2px(100);
			int padd=DensityUtil.dip2px(5);
			for (String string : imgs) {
				ImageView imageView=new ImageView(getApplicationContext());
				imageView.setScaleType(ScaleType.CENTER_CROP);
				ViewGroup.MarginLayoutParams layoutParams= new ViewGroup.MarginLayoutParams(width, height);
				layoutParams.leftMargin=padd;
				imgsContainer.addView(imageView,layoutParams );
				 
				XImageLoader.load(NetPublicConstant.IMAGE_URL+string, imageView);
			}
		}
		
		xlistview.addHeaderView(headerView);
		
		adapter=new ChaKanGengDuoPingLunAdapter((String)getIntent().getStringExtra("fid"),dataList);
		xlistview.setAdapter(adapter);
//		
//		intent.putExtra("tid",bean.getTid());
//		intent.putExtra("pid", bean.getPid());
//		intent.putExtra("imgs", bean.getImg());
//		intent.putExtra("author", bean.getAuthor());
//		intent.putExtra("authorId", bean.getAuthorId());
//		intent.putExtra("subject", bean.getSubject());
//		intent.putExtra("datelineShow", bean.getDatelineShow());
//		
//		intent.putExtra("message", bean.getMessage());
		
		findViewById(R.id.back).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
	}
	private void pullDown() {
		pageNum = 1;
		MyJsonRequest<AllCommontBean>request=new  MyJsonRequest.Builder<AllCommontBean>()
				.apiVer("100")
				.typeKey("BY_ForumPost_commentQuery")
				.param("tid", getIntent().getStringExtra("tid"))
				.param("pid",  getIntent().getStringExtra("pid"))
				.param("curpage",pageNum)
			 
				.requestListener(new XRequestListener<AllCommontBean>() {
					
					@Override
					public void onResponse(AllCommontBean arg0) {
						pageNum++;
						xlistview.stopRefresh();
						if (arg0.getCount()>0) {
							dataList.clear();
							dataList.addAll(arg0.getList());
							adapter.notifyDataSetChanged();
						}
						
						
					}
				})
				.errorListener(new XErrorListener() {
					
					@Override
					public void onErrorResponse(Exception exception, int code, String msg) {
						xlistview.stopRefresh();
					}
				})
				.build();
		
		
		HttpRequest.getDefaultRequestQueue().add(request);
	}
	
	private void pullUp() {
	 
		MyJsonRequest<AllCommontBean>request=new  MyJsonRequest.Builder<AllCommontBean>()
				.apiVer("100")
				.typeKey("BY_ForumPost_commentQuery")
				.param("tid", getIntent().getStringExtra("tid"))
				.param("pid",  getIntent().getStringExtra("pid"))
				.param("curpage",pageNum)
			 
				.requestListener(new XRequestListener<AllCommontBean>() {
					
					@Override
					public void onResponse(AllCommontBean arg0) {

						pageNum++;
						xlistview.stopLoadMore();
						
						if (arg0.getCount()>0) {
							dataList.addAll(arg0.getList());
							adapter.notifyDataSetChanged();
						}
					}
				})
				.errorListener(new XErrorListener() {
					
					@Override
					public void onErrorResponse(Exception exception, int code, String msg) {
						xlistview.stopLoadMore();
					}
				})
				.build();
		
		
		HttpRequest.getDefaultRequestQueue().add(request);
	}

}
